Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[base-ui] Create useNumberInput and NumberInput #36119

Merged
merged 70 commits into from
Aug 3, 2023

Conversation

mj12albert
Copy link
Member

@mj12albert mj12albert commented Feb 9, 2023

This PR will add useNumberInput and NumberInput to base.

One step toward closing #19154.

Summary

  • useNumberInput implements a number field component that currently supports positive and negative integers, and buttons that can increment and decrement the value. It's mostly adapted from useInput.
  • NumberInput uses useNumberInput internally and is the component counterpart.
  • In addition to the native onChange of the inner <input>, a onValueChange callback is provided that only fires when the value changes to a valid (clamped) value
  • I added @testing-library/user-event as some important keyboard interactions like inputting characters one by one and testing each change handler do not work with plain fireEvent
  • Last minute change: no role="spinbutton" because it doesn't work well with VO (see SpinButton doesn't increment or decrement on Voiceover VO controls microsoft/fluentui#13606)

Preview: https://deploy-preview-36119--material-ui.netlify.app/base/react-number-input/

@mj12albert mj12albert added the package: base-ui Specific to @mui/base label Feb 9, 2023
@mui-bot
Copy link

mui-bot commented Feb 9, 2023

Netlify deploy preview

@material-ui/unstyled: parsed: +4.79% , gzip: +4.71%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 420053d

@mj12albert mj12albert force-pushed the base/number-input branch 9 times, most recently from c4a5b72 to df004fe Compare February 22, 2023 14:57
@mj12albert
Copy link
Member Author

mj12albert commented Feb 24, 2023

When should onChange be called?

@mui/core After using my own implementation, I'm starting to think it's not the best 🥲. I wrote up alternatives in the PR description above, would appreciate your thoughts the options I've came up with or other suggestions!

@mj12albert mj12albert requested a review from mnajdova February 24, 2023 11:25
Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of the open question regarding the onChange fire, I would recommend keeping it similar with how the native input number is working. People can still use onBlur if they want behavior similar to the other option.

@mj12albert mj12albert force-pushed the base/number-input branch 13 times, most recently from b6faf9a to 90777ca Compare March 2, 2023 06:17
@mj12albert
Copy link
Member Author

mj12albert commented Aug 1, 2023

@michaldudak @mnajdova I've addressed the remaining comments!

In particular:

  • To fix the import paths, I modified the docs pages components: b887e89
  • I noticed native CSS nesting doesn't work in Firefox yet (it's behind an experimental flag) and opened a separate issue: [docs][base-ui] Plain CSS nesting not supported in Firefox #38266
  • After failing to align the arrows properly with CSS, I found it there may be an issue with the "small downward arrow" in Plex Sans... ended up using system-ui for arrows

Screenshot 2023-08-01 at 9 38 16 PM

@mj12albert mj12albert requested a review from mnajdova August 1, 2023 15:37
@michaldudak
Copy link
Member

The component import is still incorrect:

import Unstable_NumberInput as NumberInput from '@mui/base/Unstable_NumberInput';

should be

import NumberInput from '@mui/base/Unstable_NumberInput';

@mj12albert
Copy link
Member Author

The component import is still incorrect:

👌 Fixed in 652c650 @michaldudak

Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one nit left, besides that, it's good to go!

There's one more issue we can tackle in a separate PR: slots and classes are not documented in the component API page.

@@ -146,9 +165,9 @@ export default function ComponentsApiContent(props) {
<Heading text="import" hash={`${componentNameKebabCase}-import`} level="h3" />
<HighlightedCode
code={`
import ${pageContent.name} from '${source}/${pageContent.name}';
import ${namedImportName} from '${namedImportPath}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the other way around?
import X from 'X' is the default import, while import { X } from 'X' is the named one.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let add the component after merging in #37222

import NumberInput, { numberInputClasses } from '@mui/base/Unstable_NumberInput';
import { styled } from '@mui/system';

const CustomNumberInput = React.forwardRef(function CustomNumberInput(props, ref) {
Copy link
Member

@mnajdova mnajdova Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's most important to have the Tailwind + Plain CSS demos on the introduction demo. It's the first thing people see when they come to the page :)

} from '@mui/base/Unstable_NumberInput';
import clsx from 'clsx';

const CustomNumberInput = React.forwardRef(function CustomNumberInput(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for introducing new component, it helps with the readibility.

@mj12albert mj12albert changed the title [base] Create useNumberInput and NumberInput [base-ui] Create useNumberInput and NumberInput Aug 3, 2023
@mj12albert mj12albert merged commit f95ad25 into mui:master Aug 3, 2023
@mj12albert mj12albert deleted the base/number-input branch August 3, 2023 10:17
richbustos pushed a commit that referenced this pull request Aug 4, 2023
richbustos pushed a commit that referenced this pull request Aug 7, 2023
@oliviertassinari oliviertassinari added new feature New feature or request component: number field This is the name of the generic UI component, not the React module! labels Aug 15, 2023
@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 15, 2023

@mj12albert Nice! A couple of opportunities I can notice in https://mui.com/base-ui/react-number-input/:

  • We had no GitHub issue for this new component, done
  • We were not linking this PR with the issue, done
Screenshot 2023-08-15 at 02 38 51 Screenshot 2023-08-15 at 02 20 33 Screenshot 2023-08-15 at 02 23 10
  • 3 Indentation issue on the docs page, should remove the tabs:
Screenshot 2023-08-15 at 02 24 32

#38521

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 3, 2023

@mj12albert Thanks for the follow-ups on my comment. Opening individual issues for each point is the way to go 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: number field This is the name of the generic UI component, not the React module! new feature New feature or request package: base-ui Specific to @mui/base
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

8 participants